/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.kernel;

import java.lang.reflect.Method;

/**
 * This interface defines a primitive dispatcher, that is an object that
 * keeps a list of primitives along with their names and allow call of them.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
interface Dispatcher {

    /**
     * Returns the names of all primitives.
     * @return the names of all primitives
     */
    String[] getNames();

    /**
     * Returns the {@link java.lang.reflect.Method} objects associated with
     * all primitives.
     * @return the methods of all primitives
     */
    Method[] getMethods();

    /**
     * Returns the name of a given primitive.
     * @param prim index of primitive to get name from
     * @return the name of the requested primitive if it exists,
     *         <tt>null</tt> otherwise
     */
    String getName(int prim);

    /**
     * Returns the {@link java.lang.reflect.Method} object associated with
     * a given primitive.
     * @param prim index of primitive to get method from
     * @return {@link java.lang.reflect.Method} object associated with
     *         the requested primitive if it exists, <tt>null</tt> otherwise
     */
    Method getMethod(int prim);

    /**
     * Returns the number of primitives in this dispatcher.
     * @return the number of primitives in this dispatcher
     */
    int size();

    /**
     * Calls a primitive with given parameters.
     * @param prim index of primitive to call
     * @param ctxt context - should not be <tt>null</tt>
     * @param params parameters to primitive - should not be <tt>null</tt>
     * @return the result of the primitive
     * @throws Fatal.Exception if the requested primitive does not exist
     * @throws Fatal.Exception if the requested primitive is not accessible
     * @throws Fail.Exception if thrown by the primitive
     * @throws Fatal.Exception if thrown by the primitive
     */
    Value invoke(int prim, CodeRunner ctxt, Value[] params)
        throws Fatal.Exception, Fail.Exception, FalseExit;

    /**
     * Adds a primitive to this dispatcher.
     * @param name primitive name - should not be <tt>null</tt>
     * @param impl primitive implementation - should not be <tt>null</tt>
     * @return the index of the added primitive
     */
    int addPrimitive(String name, Method impl);

} // end interface 'Dispatcher'
